/*
 * main.c
 *
 *  Created on: 2011-11-3
 *      Author: Octopus
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <math.h>
#include <windows.h>

#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/gl.h>

#include "gl_objects.h"
#include "gl_utils.h"

void setupRenderStatus(){
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glEnable(GL_DEPTH_TEST);
	glFrontFace(GL_CCW);
	glEnable(GL_CULL_FACE);

	GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
	glMateriali(GL_FRONT, GL_SHININESS, 4);
	glMaterialfv(GL_FRONT, GL_SPECULAR, specular);

	GLfloat ambientLight[] = {0.0f, 0.0f, 0.0f, 1.0f};
	GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
	GLfloat specularLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
	glEnable(GL_LIGHT0);

	glShadeModel(GL_FLAT);

//	glEnable(GL_NORMALIZE);		//no need to normalize as I did it already.
}
static GLfloat rotX = 0.0f;
static GLfloat rotY = 0.0f;
static GLfloat rotZ = 0.0f;
GLfloat lightPosition[] = {0.0f, 150.0f, 0.0f, 1.0f};
void display() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushMatrix();
	glMatrixMode(GL_MODELVIEW);

	//view point(0.0f, 00.0f, 500.0f), rotated;
	glTranslatef(0.0f, 0.0f, -500.0f);
	glRotatef(rotY, 1.0f, 0.0f, 0.0f);
	glRotatef(rotX, 0.0f, 0.1f, 0.0f);
	glRotatef(rotZ, 0.0f, 0.0f, 0.1f);

	GLubyte color1[3] = {0, 0, 0};
	GLubyte color2[3] = {255, 255, 255};
	drawGridPlane(100.0f, 100.0f, 10, 10, color1, color2);

	glPushMatrix();
		GLfloat equ[4];
		GLfloat vertexes[] = {
			0, 0.1f, 0,
			1, 0.1f, 0,
			0, 0.1f, 1
		};
		createPlaneEquation(equ, vertexes);
		GLfloat shadowMatrix[16];
		GLfloat light[] = {-50.0f, -150.0f, -50.0f};
		createShadowMatrix(shadowMatrix, equ, light);
		glMultMatrixf(shadowMatrix);
		glTranslatef(0.0f, 50.0f, 0.0f);
		glColor3ub(255, 255, 0);
		drawJet();
	glPopMatrix();

	glPushMatrix();
		glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
		glColor3ub(255, 255, 255);
		glutSolidSphere(5.0f, 10, 10);
	glPopMatrix();
	glEnable(GL_LIGHTING);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

	glPushMatrix();
		glTranslatef(0.0f, 50.0f, 0.0f);
		glColor3ub(255, 0, 0);
		drawJet();
	glPopMatrix();

	glDisable(GL_LIGHTING);

	glPopMatrix();
	glutSwapBuffers();
}
void reshape(GLsizei w, GLsizei h){
	GLfloat aspectRatio;
	if(h == 0)
		h = 1;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	aspectRatio = (GLfloat)w / (GLfloat)h;
	gluPerspective(45.0f, aspectRatio, 1.0f, 1000.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
static int orginX = 0;
static int orginY = 0;
static GLboolean pressed = GL_FALSE;
void mouseFunc(int button, int state, int x, int y){
	if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
		pressed = GL_TRUE;
		orginX = x;
		orginY = y;
	}else if(button == GLUT_LEFT_BUTTON && state == GLUT_UP){
		pressed = GL_FALSE;
	}
}
void motionFunc(int x, int y){
	if(!pressed)
		return;
	int deltaX = x - orginX;
	int deltaY = y - orginY;
	rotX += deltaX / 10.0f;
	rotY += deltaY / 10.0f;
	orginX = x;
	orginY = y;
	glutPostRedisplay();
}
int main(int argc, char **argv) {

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

	glutCreateWindow("Hello,world!");
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMouseFunc(mouseFunc);
	glutMotionFunc(motionFunc);
	setupRenderStatus();

	glutMainLoop();
	return 0;
}





